﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Admin;
using BusinessObjects;
using System.Data;
using DataObjects.AdoNet;

namespace DataObjects.SqlServer.Admin
{
    public class SqlServerUserGroupDao : IUserGroupDao
    {
        public List<UserGroup> GetUserGroups()
        {
            string procedures = "Get_All_UserGroups";

            return Db.ReadList(procedures, true, Make);
        }

        public List<UserGroup> GetUserGroupsNoImage()
        {
            string procedures = "Get_All_UserGroups_NoImage";

            return Db.ReadList(procedures, true, MakeNoImage);
        }

        public UserGroup GetUserGroup(string id)
        {
            string procedures = "Get_All_UserGroups_ByID";

            object[] parms = { "@UserGroupID", id };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<UserGroup> GetUserGroupByBranch(string restaurantId)
        {
            string procedures = "Get_All_UserGroups_ByBranch";

            object[] parms = { "@RestaurantID", restaurantId };
            return Db.ReadList(procedures, true, MakeNoImage, parms);
        }

        public void InsertUserGroup(UserGroup userGroup)
        {
            string sql = "Insert_UserGroup";

            userGroup.UserGroupID = Db.Insert(sql, true, Take(userGroup));
        }

        public void UpdateUserGroup(UserGroup userGroup)
        {
            string sql = "Update_UserGroup";

            Db.Update(sql, true, Take(userGroup));
        }

        public void DeleteUserGroup(UserGroup userGroup)
        {
            string sql = "Delete_UserGroup";

            Db.Update(sql, true, Take(userGroup.UserGroupID));
        }

        private static Func<IDataReader, UserGroup> Make = reader =>
           new UserGroup
           {
               UserGroupID = reader["UserGroupID"].AsString(),
               UserGroupName = reader["UserGroupName"].AsString(),
               Image = (byte[])reader["Image"],
               RestaurantID = reader["RestaurantID"].AsString(),
               CreateBy = reader["CreateBy"].AsString(),
               CreateDate = reader["CreateDate"].AsDateTime(),
               ModifiedBy = reader["ModifiedBy"].AsString(),
               ModifiedDate = reader["ModifiedDate"].AsDateTime(),
               Description = reader["Description"].AsString(),
               IsActive = reader["IsActive"].AsBool()
           };

        private static Func<IDataReader, UserGroup> MakeNoImage = reader =>
            new UserGroup
            {
                UserGroupID = reader["UserGroupID"].AsString(),
                UserGroupName = reader["UserGroupName"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString(),
                CreateBy = reader["CreateBy"].AsString(),
                CreateDate = reader["CreateDate"].AsDateTime(),
                ModifiedBy = reader["ModifiedBy"].AsString(),
                ModifiedDate = reader["ModifiedDate"].AsDateTime(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };

        private object[] Take(UserGroup userGroup)
        {
            return new object[]  
            {
                "@UserGroupID", userGroup.UserGroupID,
                "@UserGroupName", userGroup.UserGroupName,
                "@Image", userGroup.Image,
                "@RestaurantID", userGroup.RestaurantID,
			    "@CreateBy", userGroup.CreateBy,
                "@CreateDate", userGroup.CreateDate ,
                "@ModifiedBy", userGroup.ModifiedBy ,
                "@ModifiedDate", userGroup.ModifiedDate ,
                "@Description", userGroup.Description ,
                "@IsActive", userGroup.IsActive ,
            };
        }

        private object[] Take(string userGroupID)
        {
            return new object[]  
            {
                "@UserGroupID", userGroupID
            };
        }
    }
}
